//
//  CZTopItem.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZTopItem.h"

@interface CZTopItem ()

/// 竖线
@property (nonatomic, strong) UIView *separatorView;

/// 按钮
@property (nonatomic, strong) UIButton *iconButton;

/// 主标题
@property (nonatomic, strong) UILabel *titleLabel;

/// 子标题
@property (nonatomic, strong) UILabel *subTitleLabel;

@end

@implementation CZTopItem

+ (instancetype)topItemWithIcon:(NSString *)icon hightedIcon:(NSString *)hIcon title:(NSString *)title subTitle:(NSString *)subTitle {
    return [[self alloc] initWithIcon:icon hightedIcon:hIcon title:title subTitle:subTitle];
}

- (instancetype)initWithIcon:(NSString *)icon hightedIcon:(NSString *)hIcon title:(NSString *)title subTitle:(NSString *)subTitle {
    if (self = [super init]) {
        [self prepareUI];
        
        // 重新设置内容
//        [_iconButton setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
//        [_iconButton setImage:[UIImage imageNamed:hIcon] forState:UIControlStateHighlighted];
//        _titleLabel.text = title;
//        _subTitleLabel.text = subTitle;
        
        self.icon = icon;
        self.hIcon = hIcon;
        self.title = title;
        self.subTitle = subTitle;
    }
    
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self prepareUI];
    }
    
    return self;
}

- (void)prepareUI {
    // 1. 添加子控件
    [self addSubview:self.separatorView];
    [self addSubview:self.iconButton];
    [self addSubview:self.titleLabel];
    [self addSubview:self.subTitleLabel];
    
    // 2. 设置约束
    [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 左边和父控件重合
        make.leading.equalTo(self);
        
        // 顶部和父控件重合,有偏移
        make.top.equalTo(self).offset(12);
        
        // 底部和父控件重合,有偏移
        make.bottom.equalTo(self).offset(-12);
        
        // 宽度等于1
//        make.width.equalTo(@(1));
        make.width.mas_equalTo(1);
    }];
    
    // 按钮填充父控件,点击父控件任何一个地方都能响应按钮的点击事件
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        // 填充父控件
//        make.edges.mas_equalTo(UIEdgeInsetsZero);
        make.edges.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(40);
        make.top.equalTo(self).offset(5);
    }];
    
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titleLabel);
        make.bottom.equalTo(self).offset(-5);
    }];
}

#pragma mark - 设置内容
- (void)setIcon:(NSString *)icon {
    _icon = icon;
    
    // 将图片设置到按钮上面
    [self.iconButton setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
}

- (void)setHIcon:(NSString *)hIcon {
    _hIcon = hIcon;
    
    // 将图片设置到按钮上面
    [self.iconButton setImage:[UIImage imageNamed:hIcon] forState:UIControlStateHighlighted];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    self.titleLabel.text = title;
}

- (void)setSubTitle:(NSString *)subTitle {
    _subTitle = subTitle;
    
    self.subTitleLabel.text = subTitle;
}

#pragma makr - 按钮点击事件
- (void)iconButtonClick {
    if ([self.delegate respondsToSelector:@selector(topItemClick:)]) {
        [self.delegate topItemClick:self];
    }
}

#pragma mark - 懒加载
- (UIView *)separatorView {
    if (_separatorView == nil) {
        _separatorView = [[UIView alloc] init];
        
        // 设置背景颜色
        _separatorView.backgroundColor = [UIColor blackColor];
        _separatorView.alpha = 0.2;
    }
    
    return _separatorView;
}

- (UIButton *)iconButton {
    if (_iconButton == nil) {
        _iconButton = [[UIButton alloc] init];
        
        // 设置默认图片
        [_iconButton setImage:[UIImage imageNamed:@"icon_district"] forState:UIControlStateNormal];
        [_iconButton setImage:[UIImage imageNamed:@"icon_district_highlighted"] forState:UIControlStateHighlighted];
        
        _iconButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _iconButton.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
        
        // 添加点击事件
        [_iconButton addTarget:self action:@selector(iconButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _iconButton;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        
        // 设置字体
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.text = @"主标题";
    }
    
    return _titleLabel;
}

- (UILabel *)subTitleLabel {
    if (_subTitleLabel == nil) {
        _subTitleLabel = [[UILabel alloc] init];
        
        // 设置字体
        _subTitleLabel.font = [UIFont systemFontOfSize:12];
        
        _subTitleLabel.text = @"子标题";
    }
    
    return _subTitleLabel;
}
@end
